package org.mspring.mlog.service;

import java.util.List;

import org.mspring.mlog.entity.Attachment;
import org.mspring.mlog.entity.constants.CacheNames;
import org.mspring.platform.cache.Cache;
import org.mspring.platform.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章附件相关服务
 *
 * @author Gao Youbo
 * @since 2014年5月16日
 */
@Service
@Transactional
public class PostAttachService {
    private AttachmentService attachmentService;
    private CacheManager cacheManager;
    private Cache postAttachListCache;

    public List<Attachment> listByPost(long postId) {
        List<Attachment> attachs = postAttachListCache.get(String.valueOf(postId));
        if (attachs != null) {
            return attachs;
        }
        attachs = attachmentService.listAttachmentsByForeign(Attachment.AttachFrom.FROM_POST, postId);
        postAttachListCache.set(String.valueOf(postId), attachs);
        return attachs;
    }

    @Autowired
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }

    @Autowired
    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        this.postAttachListCache = this.cacheManager.getCache(CacheNames.POST_ATTACH_LIST_CACHE);
    }

}
